package health_system.health_system.dto;

import health_system.health_system.entity.Moment;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.List;

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class MomentWithStatsDTO {
    private Long id;
    private Long userId;
    private String username;
    private String userAvatarUrl;
    private String content;
    private Long likeCount;
    private Long commentCount;
    private Long shareCount;
    private Moment.VisibilityType visibility;
    private Integer visibilityDuration;
    private Boolean isLikedByCurrentUser;
    private List<MomentMediaDTO> mediaList;
    private LocalDateTime createdAt;
    private LocalDateTime updatedAt;
    
    public static MomentWithStatsDTO fromMomentAndStats(Moment moment, Long likeCount) {
        if (moment == null) {
            return null;
        }
        
        MomentWithStatsDTO dto = new MomentWithStatsDTO();
        dto.setId(moment.getId());
        
        if (moment.getUser() != null) {
            dto.setUserId(moment.getUser().getId());
            dto.setUsername(moment.getUser().getUsername());
            dto.setUserAvatarUrl(moment.getUser().getAvatarUrl());
        }
        
        dto.setContent(moment.getContent());
        dto.setLikeCount(likeCount);
        dto.setCommentCount(moment.getComments() != null ? (long) moment.getComments().size() : 0L);
        dto.setShareCount(moment.getShares() != null ? (long) moment.getShares().size() : 0L);
        dto.setVisibility(moment.getVisibility());
        dto.setVisibilityDuration(moment.getVisibilityDuration());
        dto.setCreatedAt(moment.getCreatedAt());
        dto.setUpdatedAt(moment.getUpdatedAt());
        
        return dto;
    }
    
    public static MomentWithStatsDTO fromMomentAndStatsWithLikeStatus(Moment moment, Long likeCount, Long currentUserId) {
        MomentWithStatsDTO dto = fromMomentAndStats(moment, likeCount);
        if (dto != null && moment.getLikes() != null) {
            dto.setIsLikedByCurrentUser(moment.getLikes().stream()
                    .anyMatch(like -> like.getUser().getId().equals(currentUserId)));
        }
        return dto;
    }
} 